﻿#include "dialog.h"
#include "ui_dialog.h"
#include <QSerialPortInfo>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);


    QList<QSerialPortInfo> list = QSerialPortInfo::availablePorts();

    foreach(const QSerialPortInfo &info , list)
    {
        ui->comboBox->addItem(info.portName());
    }

    ui->comboBox->setCurrentIndex(0);
}



Dialog::~Dialog()
{
    m_lc.turn_off(0);
    m_lc.turn_off(1);
    m_lc.close();
    delete ui;
}


void Dialog::on_pushButtonOpen_clicked()
{
    QString comm = ui->comboBox->currentText();
    if(comm.isEmpty()) return;

    qDebug() << "on_pushButtonOpen_clicked";
    m_lc.open(comm);
//    m_lc.turn_on(0);
//    m_lc.turn_on(1);

    uint8_t v1, v2;
    m_lc.readLight(0, v1);
    m_lc.readLight(1, v2);
    ui->horizontalSliderCH1->setValue(v1);
    ui->horizontalSliderCH2->setValue(v2);

}

void Dialog::on_horizontalSliderCH1_valueChanged(int value)
{
    ui->spinBoxCH1->setValue(value);
    m_lc.setLight(0, value);
}

void Dialog::on_horizontalSliderCH2_valueChanged(int value)
{
    ui->spinBoxCH2->setValue(value);
    m_lc.setLight(1, value);
}

void Dialog::on_checkBoxCH1_toggled(bool checked)
{
    if(checked)     m_lc.turn_on(0);
    else
    {
        m_lc.turn_off(0);
        uint8_t v1;
        m_lc.readLight(0, v1);
        ui->horizontalSliderCH1->setValue(v1);
    }

}


void Dialog::on_checkBoxCH2_toggled(bool checked)
{
    if(checked)     m_lc.turn_on(1);
    else
    {
        m_lc.turn_off(1);
        uint8_t v1;
        m_lc.readLight(1, v1);
        ui->horizontalSliderCH2->setValue(v1);
    }
}

